package com.yun2.bms.dao.impl;

import com.yun2.bms.dao.IBookDao;
import com.yun2.bms.model.Book;
import com.yun2.bms.model.Category;
import com.yun2.bms.model.Lend;
import com.yun2.bms.utils.DBUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * author: youli
 * qq: 27855162@qq.com
 * date: 2020/5/26
 */

public class BookDaoImpl extends BaseDaoImpl implements IBookDao {
    @Override
    public List<Book> getAll() {
        List<Book> list = new ArrayList<Book>();
        try {
            String sql = "select * from book";
            Object[] params = {};
            super.queryBySql(sql, params);
            while (rs.next()) {
                Book book = new Book();
                makeModel(book);
                list.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn, st, rs);
        }
        return list;
    }

    @Override
    public int add(Book book) {
        String sql = "insert into book(book_name,book_number,category_id,author,publisher,price)values(?,?,?,?,?,?)";
        Object[] params = {book.getBookName(), book.getBookNumber(), book.getCategoryId(),
                book.getAuthor(), book.getPublisher(), book.getPrice(),};
        return super.insertBySql(sql, params);
    }

    @Override
    public int edit(Book book) {
        String sql = "update book b set b.book_name = ?,b.book_number = ?, b.author = ?, " +
                "b.publisher = ?, b.price = ?, b.category_id = ? where b.id = ?";
        Object[] params = {book.getBookName(), book.getBookNumber(), book.getAuthor(),
                book.getPublisher(), book.getPrice(), book.getCategoryId(), book.getId()};
        return super.updateBySql(sql, params);
    }

    @Override
    public int delete(String ids) {
        String sql = "delete from book where id in (?)";
        Object[] params = {ids};
        return super.updateBySql(sql, params);
    }

    @Override
    public List<Book> getAllByBookName(String bookName) {
        List<Book> list = new ArrayList<Book>();
        try {
            String sql = "select * from book where book_name like ? or author like ? or publisher like ?";
            String value = "%" + StringUtils.trim(bookName) + "%";
            Object[] params = {value, value, value};
            super.queryBySql(sql, params);
            while (rs.next()) {
                Book book = new Book();
                makeModel(book);
                list.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn, st, rs);
        }
        return list;
    }

    @Override
    public Book get(int id) {
        Book book = null;
        try {
            String sql = "select * from book where id = ?";
            Object[] params = {id};
            super.queryBySql(sql, params);
            System.out.println("rs===="+rs);
            while (rs.next()) {
                book = new Book();
                makeModel(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn, st, rs);
        }
        return book;
    }

    private void makeModel(Book book) throws SQLException {
        book.setId(rs.getInt("id"));
        book.setBookName(rs.getString("book_name"));
        book.setAuthor(rs.getString("author"));
        book.setPublisher(rs.getString("publisher"));
        book.setPrice(rs.getDouble("price"));
        book.setBookNumber(rs.getInt("book_number"));
        book.setCategoryId(rs.getInt("category_id"));
    }
}
